// pages/script/script.js
const request = require('../../utils/request')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    queryParam: {
      currPage: 1,
      pageSize: 10,
    },
    isPeopleActive: "all",
    isScriptActive: "全部",
    isTypeActive: "all",
    peopleList: [{
      key: "all",
      value: '全部'
    }, {
      key: 4,
      value: '≤4人本'
    }, {
      key: 5,
      value: '5人本'
    }, {
      key: 6,
      value: '6人本'
    }, {
      key: 7,
      value: '7人本'
    }, {
      key: 8,
      value: '8人本'
    }, {
      key: 9,
      value: '9人本'
    }, {
      key: 10,
      value: '10+人本'
    }],
    typeOptions: [{
        key: "all",
        value: "全部",
      }, {
        key: 0,
        value: "盒装",
      },
      {
        key: 1,
        value: "城限",
      },
      {
        key: 2,
        value: "独家",
      },
    ],
    scriptTypeOptions: [
      "全部",
      "情感",
      "硬核",
      "机制",
      "恐怖",
      "欢乐",
      "阵营",
      "还原",
      "推理",
      "立意",
      "现代",
      "日式",
      "本格",
      "变格",
      "欧式",
      "民国",
      "微恐",
      "进阶",
      "烧脑",
      "半封闭",
      "架空",
      "古风",
      "玄幻",
      "神话",
      "悬疑"
    ],
    scriptList: [],
    total: 0,
    isTest: false,
    focus: true,
    searchName: '',
    /**
     * 页面的初始数据
     */
    navBarHeight: app.globalData.navBarHeight, // 获取导航栏高度
    menuRight: app.globalData.menuRigh, // 获取胶囊距右方间距（方保持左、右间距一致）
    menuTop: app.globalData.menuTop, // 获取胶囊距底部间距（保持底部间距一致）
    menuHeight: app.globalData.menuHeight, // 获取胶囊高度（自定义内容可与胶囊高度保证一致）
  },
  handleClick(e) {
    console.log('点击', e);
    const {
      type,
      value
    } = e.target.dataset
    if (type == 'person') {
      this.setData({
        isPeopleActive: value,
        isTest: true
      })
      this.getList()
    } else if (type == 'scriptType') {
      this.setData({
        isScriptActive: value,
      })
      this.getList()
    } else {
      this.setData({
        isTypeActive: value,
      })
      this.getList()
    }

  },
  lower(e) {
    console.log('触底数据', this.data.scriptList.length, this.data.total);
    if (this.data.scriptList.length < this.data.total) {
      this.setData({
        "queryParam.currPage": this.data.queryParam.currPage + 1
      })

      this.getPushList()
    }
  },
  getList() {
    const that = this
    const token = wx.getStorageSync('token')
    this.data.queryParam = {
      currPage: 1,
      pageSize: 10,
      number: this.data.isPeopleActive != 'all' ? this.data.isPeopleActive : '',
      scriptType: this.data.isScriptActive != '全部' ? this.data.isScriptActive : '',
      type: this.data.isTypeActive != 'all' ? this.data.isTypeActive : '',
      name: this.data.searchName,
      token:token || ''
    }
    request({
      url: '/script',
      data: that.data.queryParam,
      success(data) {
        console.log('请求成功：', data);
        data.data.records.forEach(item => {
          item.scriptType = item.scriptType.split(',')
          item.isLike = false
        });
        console.log('请求成功1：', data.data.total);
        that.setData({
          scriptList: data.data.records,
          total: data.data.total,
        })
      },
      fail(err) {
        console.log('请求失败：', err);
      }
    })
  },
  getPushList() {
    const that = this
    const token = wx.getStorageSync('token')
    this.data.queryParam = {
      ...this.data.queryParam,
      number: this.data.isPeopleActive != 'all' ? this.data.isPeopleActive : '',
      scriptType: this.data.isScriptActive != '全部' ? this.data.isScriptActive : '',
      type: this.data.isTypeActive != 'all' ? this.data.isTypeActive : '',
      token:token || ''
    }
    request({
      url: '/script',
      data: that.data.queryParam,
      success(data) {
        const list = that.data.scriptList
        console.log('之前数组', list);
        data.data.records.forEach(item => {
          item.scriptType = item.scriptType.split(',')
          item.isLike = false
          list.push(item)
        });
        console.log('push新数组', list);
        that.setData({
          scriptList: list
        })

      },
      fail(err) {
        console.log('请求失败：', err);
      }
    })
  },
  onInput(e) {
    this.setData({
      searchName: e.detail.value, // 将输入框的内容赋值给绑定的属性
    })
  },
  search() {
    this.getList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const isHome = wx.getStorageSync('isHome')
    if (isHome) {
      this.setData({
        focus: true
      })
    } else {
      this.setData({
        focus: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  onShareTimeline(){}
})